package com.feature.core.rabbitmq;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;

import com.feature.core.util.TraceIdUtil;

/**
 * Spring 中的消息后置处理器通常是在消息发送或接收的 MessageListenerContainer 中注册的，可以在消息发送或接收前后执行某些操作。
 * 
 * @Description:
 * @author wangzhiyue
 * @date 2025/04/29 10:06:19
 */
public class AfterReceivePostProcessor implements MessagePostProcessor {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        MessageProperties messageProperties = message.getMessageProperties();
        try {
            logger.info("接收到消息,MSGID:{},body:{}", messageProperties.getMessageId(), new String(message.getBody(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String traceID = String.valueOf(messageProperties.getHeaders().get("traceID"));
        TraceIdUtil.setTraceID(traceID);
        return message;
    }

}
